<?php
class IndexController extends  Controller{

	public function index(){
		$this->getSmarty();
		$this->getTemplatePosts();
		$this->getTemplateHotPosts();

		require_once SERVICE . DS . 'ad' . DS . 'AdService.class.php';
		$service = new AdService ( $this->getDB () );
		$curCategory_name = $service->getImgshowHtml();
        $this->smarty->assign ( 'curCategory_name', $curCategory_name );
        require_once COMMON . DS  . 'Constant.class.php';
        $this->smarty->assign ( 'tags',Constant::getHotTag() );
		$this->smarty->assign ( 'pagesets', Constant::indexHeader() );
		$this->smarty->display("index.tpl");
	}

	/**
	 *
	 * 获取文章前10条数据
	 */


	public function getTemplatePosts(){
		require_once SERVICE . DS . 'IndexService.class.php';

		$indexService = new IndexService( $this->getDB () );
		
        $result = $indexService->loadPosts4ListCategory(0,30);
		
		$this->smarty->assign ( 'posts_html', $result );
	}

	public function getTemplateHotPosts(){
		require_once SERVICE . DS . 'IndexService.class.php';

		$indexService = new IndexService( $this->getDB () );
		
		$order = "order by  click_count desc ";
		
        $result = $indexService->loadHotPosts(0, 25, $order);
		
		$this->smarty->assign ( 'hotResult', $result );
	}
	
	/**
	 *
	 * 点击查看更多加载数据(文章)
	 */
	public function loadPosts(){
		$this->getSmarty();
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 0;
        $rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 30;
        $click_count = isset ( $_POST ['order'] ) ? $_POST ['order'] : "";
        require_once SERVICE . DS . 'IndexService.class.php';
        $indexService = new IndexService( $this->getDB () );
		
        $order = empty($click_count)?"order by  post_date desc ":'order by  '.$click_count.' desc';
        $result = $indexService->loadPosts4ListCategory($page, $rows, $order, "", "load");
        echo json_encode ( $result );
	}


	public function loadHotPost(){
		$this->getSmarty();
		require_once COMMON . DS  . 'Constant.class.php';
        require_once SERVICE . DS . 'IndexService.class.php';
        $indexService = new IndexService( $this->getDB () );

        $order = "order by  click_count desc ";
        $result = $indexService->loadHotPosts(0, 25, $order, "load");

  		 foreach ($result as $k => $val) {
			$val->post_file_location = Constant::getAmazonPic($val->post_file_location,180);
       	}
        echo json_encode ( $result );
	}

}